#include "pw_import_store.h"
#include "hash_table.h"
#include "struct_ul.h"
#include <fstream>
bool Import_User_Hash_table(const char *filepath){
    fstream Read(filepath,ios::in|ios::binary);
    if(!Read)return false;
    ID_PASSWORD temp;
    Read.seekg(0,ios::end);
    auto end = Read.tellg();
    Read.seekg(0,ios::beg);
    auto size = end / (sizeof(ID_PASSWORD));
    while (size--) {
        Read.read((char *)&temp,sizeof(ID_PASSWORD));
        user_password_id.add(temp.key,temp.value);
    }
    Read.close();
    return true;
}
bool Import_User_Manager_Hash_table(const char *filepath){
    fstream Read(filepath,ios::in|ios::binary);
    if(!Read)return false;
    ID_PASSWORD temp;
    Read.seekg(0,ios::end);
    auto end = Read.tellg();
    Read.seekg(0,ios::beg);
    auto size = end / (sizeof(ID_PASSWORD));
    while (size--) {
        Read.read((char *)&temp,sizeof(ID_PASSWORD));
        user_manager_password.add(temp.key,temp.value);
    }
    Read.close();
    return true;
}
bool Import_Script_Manager_Hash_table(const char *filepath){
    fstream Read(filepath,ios::in|ios::binary);
    if(!Read)return false;
    ID_PASSWORD temp;
    Read.seekg(0,ios::end);
    auto end = Read.tellg();
    Read.seekg(0,ios::beg);
    auto size = end / (sizeof(ID_PASSWORD));
    while (size--) {
        Read.read((char *)&temp,sizeof(ID_PASSWORD));
        script_manager_password.add(temp.key,temp.value);
    }
    Read.close();
    return true;
}
bool Store_User_Hash_table(const char *filepath){
    fstream Write(filepath,ios::out|ios::binary);
    if(!Write)return false;
    ID_PASSWORD temp;
    for(int i = 1;i <= user_password_id.size;i++){
       temp.key = user_password_id.data[i].key;
       temp.value = user_password_id.data[i].value;
       Write.write((char *)&temp,sizeof(ID_PASSWORD));
    }
    Write.close();
    return true;
}
bool Store_User_Manager_Hash_table(const char* filepath){
    fstream Write(filepath,ios::out|ios::binary);
    if(!Write)return false;
    ID_PASSWORD temp;
    for(int i = 1;i <= user_manager_password.size;i++){
        temp.key = user_manager_password.data[i].key;
        temp.value = user_manager_password.data[i].value;
        Write.write((char *)&temp,sizeof(ID_PASSWORD));
    }
    Write.close();
    return true;
}
bool Store_Script_Manager_Hash_table(const char* filepath){
    fstream Write(filepath,ios::out|ios::binary);
    if(!Write)return false;
    ID_PASSWORD temp;
    for(int i = 1;i <= script_manager_password.size;i++){
        temp.key = script_manager_password.data[i].key;
        temp.value =script_manager_password.data[i].value;
        Write.write((char *)&temp,sizeof(ID_PASSWORD));
    }
    Write.close();
    return true;
}
